home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 May: Tool Chest / Dev.CD May 97 TC.toast / Sample Code / Snippets / Interapplication Communication / SendFinderOpen / SendFinderOpen.c < prev   
Encoding:
C/C++ Source or Header  |  1992-07-15  |  5.6 KB  |  145 lines  |  [TEXT/MPS ]

  1. /* This snippet shows you how send a Finder OpenSelection event. */
  2. /* A Finder OpenSelection allows you to have the Finder launch an Application, or */
  3. /* open a document, which will of course cause the owning Application to launch and */
  4. /* open that document.  Kinda just like the user had double-clicked on the file */
  5. /* from a Finder window. */
  6. /* This is an alternate to using the LaunchWithDoc code (somewhere else on this CD ) */
  7. /* which shows you how to launch an application with a document.  Look for that sample and */
  8. /* contrast the two methods, and pick the best for your use. */
  9. /* C.K. Haun */
  10. /* Apple Developer Tech Support */
  11.  
  12. /* include the ususal suspects, all the .h's you need  */
  13. #include <Types.h>
  14. #include <memory.h>
  15. #include <Packages.h>
  16. #include <Errors.h>
  17. #include <quickdraw.h>
  18. #include <fonts.h>
  19. #include <dialogs.h>
  20. #include <windows.h>
  21. #include <menus.h>
  22. #include <events.h>
  23. #include <OSEvents.h>
  24. #include <Desk.h>
  25. #include <diskinit.h>
  26. #include <OSUtils.h>
  27. #include <resources.h>
  28. #include <toolutils.h>
  29. #include <AppleEvents.h>
  30. #include <EPPC.h>
  31. #include <GestaltEqu.h>
  32. #include <PPCToolbox.h> 
  33. #include <Processes.h>
  34. #include <Aliases.h>
  35. #include <Files.h>
  36.  
  37. #define aeSelectionKeyword         'fsel'
  38. #define aeOpenSelection 'sope'
  39. #define kFinderSig 'FNDR'
  40. #define kSystemType 'MACS'
  41. /* prototype for process finding routine */
  42. OSErr FindAProcess(OSType typeToFind, OSType creatorToFind, ProcessSerialNumberPtr processSN);
  43.  
  44. /* OpenSelection takes a FSSpec pointer, and creates a Finder Open Selection */
  45. /* AppleEvent for the document described by the FSSpec.  This can be an */
  46. /* application or document. */
  47. OSErr OpenSelection(FSSpecPtr theDocToOpen)
  48. {
  49. /* temp variables I'll be using */
  50. AppleEvent aeEvent;                                         /* the event to create */
  51. AEDesc myAddressDesc, aeDirDesc, listElem;                  /* some descriptors I'll need */
  52. FSSpec dirSpec;                                             /* FSSpec for the 'parent' directory of the file I'm opening */
  53. AEDesc fileList;                                            /* my list */
  54. OSErr myErr;                                                /* guess */
  55. ProcessSerialNumber process;                                /* This will hold the process serial number of the Finder */
  56. AliasHandle DirAlias, FileAlias;                            /* some aliases */
  57.  
  58. /* go find the Finder's process information, please */
  59. if (FindAProcess(kFinderSig, kSystemType, &process))
  60.     return(procNotFound);     /* if I can't find the Finder, quit.  Always check this, someone else */
  61. /* could have shut down the finder */
  62. /* Create an address descriptor so the AppleEvent manager knows where to send this event */
  63. if (myErr = AECreateDesc(typeProcessSerialNumber, (Ptr)&process, sizeof(process), &myAddressDesc))
  64. return(myErr);
  65.  
  66. /* Create the empty FinderEvent */
  67. /* it's a Finder 'FNDR', Open Selection 'sope' event */
  68. if (myErr = AECreateAppleEvent(kFinderSig, aeOpenSelection, &myAddressDesc, kAutoGenerateReturnID, kAnyTransactionID, &aeEvent))
  69. return(myErr);
  70.  
  71. /* make a FSSpec for the parent folder (see the OpenSeletion description in the AE Registry ) */
  72. /* using the information in the document FSSpec */
  73. FSMakeFSSpec(theDocToOpen->vRefNum, theDocToOpen->parID, nil, &dirSpec);
  74. NewAlias(nil, &dirSpec, &DirAlias);
  75.  
  76. /* Create alias for file */
  77. NewAlias(nil, theDocToOpen, &FileAlias);
  78. /* Create the file  list */
  79. if (myErr = AECreateList(nil, 0, false, &fileList))
  80. return(myErr);
  81.  
  82. /*  create the folder  descriptor */
  83. HLock((Handle)DirAlias);
  84. AECreateDesc(typeAlias, (Ptr)*DirAlias, GetHandleSize((Handle)DirAlias), &aeDirDesc);
  85. HUnlock((Handle)DirAlias);
  86. DisposHandle((Handle)DirAlias);
  87. /* put the Directory Desc in the event as the direct object */
  88. if ((myErr = AEPutParamDesc(&aeEvent, keyDirectObject, &aeDirDesc)) == noErr)
  89. {
  90.     /* done with the desc, kill it */
  91.     AEDisposeDesc(&aeDirDesc)
  92.         ;
  93.     /*  create the file descriptor and add to aliasList */
  94.     HLock((Handle)FileAlias);
  95.     AECreateDesc(typeAlias, (Ptr)*FileAlias, GetHandleSize((Handle)FileAlias), &listElem);
  96.     HLock((Handle)FileAlias);
  97.     DisposHandle((Handle)FileAlias);
  98.     myErr = AEPutDesc(&fileList, 0, &listElem);
  99. }
  100.  
  101. if (myErr)
  102. return(myErr);
  103. AEDisposeDesc(&listElem);
  104. /* Add the file alias list to the event */
  105. if (myErr = AEPutParamDesc(&aeEvent, aeSelectionKeyword, &fileList))
  106. return(myErr);
  107. myErr = AEDisposeDesc(&fileList);
  108.  
  109. /* And now send the event! */
  110. myErr = AESend(&aeEvent, nil, kAENoReply + kAEAlwaysInteract + kAECanSwitchLayer, kAENormalPriority, kAEDefaultTimeout, nil, nil);
  111.  
  112. /* and kill the memory used */
  113. AEDisposeDesc(&aeEvent);
  114. }
  115.  
  116.  
  117. /* This runs through the process list looking for the indicated application */
  118. OSErr FindAProcess(OSType typeToFind, OSType creatorToFind, ProcessSerialNumberPtr processSN)
  119. {
  120.     ProcessInfoRec tempInfo;
  121.     FSSpec procSpec;
  122.     Str31 processName;
  123.     OSErr myErr = noErr;
  124.     /* nul out the PSN so we're starting at the beginning of the list */
  125.     processSN->lowLongOfPSN = kNoProcess;
  126.     processSN->highLongOfPSN = kNoProcess;
  127.     /* initialize the process information record */
  128.     tempInfo.processInfoLength = sizeof(ProcessInfoRec);
  129.     tempInfo.processName = &processName;
  130.     tempInfo.processAppSpec = &procSpec;
  131.     /* loop through all the processes until we */
  132.     /* 1) find the process we want */
  133.     /* 2) error out because of some reason (usually, no more processes */
  134.     do {
  135.         myErr = GetNextProcess(processSN);
  136.         if (myErr == noErr)
  137.             GetProcessInformation(processSN, &tempInfo);
  138.     }
  139.             while ((tempInfo.processSignature != creatorToFind || tempInfo.processType != typeToFind) ||
  140.                    myErr != noErr);
  141.     return(myErr);
  142. }
  143.  
  144.  
  145.